<?php

namespace app\Store\controller;
use think\Request;
use think\Validate;
use think\Db;

class Store extends ApiCommon
{
    public function storeinfo()
    {
    	$storeid = input('storeid/d',0);
		$info = get_store_info($storeid);
		$this->returnMsg('ok','',$info);
    }
	
	//控制台首页
	public function consoleIndex(){
		$storeid = input('storeid/d',1);
		session('storeid',$storeid);
		$adarr = Db::query("select * from ml_ad where storeid = $storeid and isshow = 1 and atype=1");
		//$adarr = getAdByType(1,$storeid);
		$this->returnMsg('ok','',$adarr);
	}
	
	//店铺首页详情
	public function storeindex($storeid){
		$storeid = input('storeid/d',0);
		//加载所有可以显示的首页广告
		$adinfo = Db::query("select ml_ad.*,id,gname,message,price from ml_ad,ml_goods where ml_ad.goodsid = ml_goods.id 
		and ml_ad.storeid = $storeid and ml_ad.isshow = 1 and atype in (5,8)");
		
		//广告类型
		$atype = Db::name('ad_type')->select();
		
		//优秀员工
		$blist = Db::name('beautician')->where(['storeid'=>$storeid,'isshow'=>1,'isbest'=>1])->select();
		
		//店铺介绍
		$sinfo = get_store_info($storeid);
		
		//技师角色
		$brole = Db::name('beautician_role')->where(['storeid'=>$storeid])->select();
		
		$this->returnMsg('ok','',['adinfo'=>$adinfo,'atype'=>$atype,'blist'=>$blist,'sinfo'=>$sinfo,'brole'=>$brole]);
	}
	
	//修改用户信息
	public function updatestore(){
		$userid = input('userid/d',0);
		
		if($userid<=0){
			$this->returnMsg('-1','userid 错误');
		}
		
		$data = input('post.');
		
		$validate = \think\Loader::validate('Store');
        if (!$validate->batch()->check($data)) {
            $error = $validate->getError();
            $error_msg = array_values($error);
            
            $this->returnMsg('-2',$error_msg);
        }
        
        $uinfo = Db::name('userinfo')->where(['userid'=>$userid])->select();
        if(count($uinfo)<=0){
        	$this->returnMsg('-1','查无此人');
        }
        
        $unionid = $uinfo[0]['unionid'];
		
		$savedata['username'] = $data['username'];
		$savedata['phone'] = $data['phone'];
		$savedata['sex'] = $data['sex'];
		$savedata['fullname'] = $data['fullname'];
		$savedata['birthday'] = $data['birthday'];
		
		Db::name('userinfo_p')->where(['unionid'=>$unionid])->save($savedata);
		$this->returnMsg('ok','修改成功');
	}
	
	public function searchStore(){
		$keyvalue = input('keyvalue/s','');
		
		if($keyvalue == '女友妆园' || $keyvalue == '17'){
			$this->returnMsg('ok','',[]);
		}
		
		$res = Db::name('store')->where(['name'=>$keyvalue])->select();
		
		if(count($res)<=0){
			if(strlen($keyvalue)<3){
				$this->returnMsg('ok','',[]);
			}
			
			$keyid = (int)substr($keyvalue,2);
			$keyid = ($keyid-12)/66;
			if($keyid<=0)
				$keyid = 0;
			//echo 'keyid='.$keyid;
			$res = Db::name('store')->where(['store_id'=>$keyid])->select();
			$this->returnMsg('ok',$keyid,$res);
		}
		$this->returnMsg('ok','',$res);
	}
}
